package pe.com.market.emarket.action;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.opensymphony.xwork2.ActionSupport;

import pe.com.market.common.util.Utilitario;
import pe.com.market.emarket.model.Product;

public class RegisterProductAction extends ActionSupport {
	
	private Product product;
	private String message;
	
	public String execute(){
		EntityManager em=Utilitario.getInstance().getEntityManager();
		EntityTransaction tx=em.getTransaction();
		String camino="success";
		if(product.getPrice()<2){
			message = "The product must not be less than $2";
			return "unsuccessful";
		}
		try{
			tx.begin();
			product.setStatus("A");
			em.persist(product);
			tx.commit();
			message="The product was added successfully";
		}catch(Exception ex){
			ex.printStackTrace();
			message="Unsuccessful Register";
			camino="error";
			tx.rollback();
		}
		return camino;
	}
		
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

}
